﻿Imports System.Collections.ObjectModel

Public Class Form1
    'Private WithEvents Resources As New UCBase.ResourceManager
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.WindowState = FormWindowState.Minimized
        Me.ShowInTaskbar = False
        ContextMenuStrip1.Enabled = True
        Timer1.Enabled = False
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Minimized
        Me.ShowInTaskbar = False
        ContextMenuStrip1.Enabled = True
        NetworkServer1.AutoIP = True
        NetworkServer1.Start()
        Label5.Text = "Offline"
        Label6.Text = NetworkServer1.ClientCount
        Label4.Text = NetworkServer1.IP
    End Sub
    Private Sub ShowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowToolStripMenuItem.Click
        Me.WindowState = FormWindowState.Normal
        Me.ShowInTaskbar = True
        ContextMenuStrip1.Enabled = False
        Timer1.Enabled = True

    End Sub
    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        If Me.WindowState = FormWindowState.Minimized Then
            ShowInTaskbar = False
            ContextMenuStrip1.Enabled = True
        End If
    End Sub
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        NotifyIcon1.Visible = False
        End
    End Sub

    Private Sub NetworkServer1_DataReceived(ByVal Client As Client, ByVal Data() As Byte) Handles NetworkServer1.DataReceived
        Dim Decoder As New NetworkTransport
        If Decoder.Decode(Data) Then
            If Decoder.ContainRemote Then
                Dim Command As String = Decoder.RemoteCommand
                Dim Value As String = Decoder.RemoteValue

                If Command = "GetDir" Then
                    Dim files As ReadOnlyCollection(Of String)
                    files = My.Computer.FileSystem.GetFiles("c:\ucs\system\projects", FileIO.SearchOption.SearchAllSubDirectories, "*.UCF")
                    NetworkServer1.Send(Client, Decoder.Encode("DirClear", ""))
                    For Each F As String In files
                        NetworkServer1.Send(Client, Decoder.Encode("Dir", IO.Path.GetFileName(F)))
                    Next
                End If
                If Command = "Load" Then
                    Dim F As String = IO.Path.Combine("c:\ucs\system\projects", Value)
                    MsgBox("Load : " & F)
                End If
            End If
        End If

    End Sub

    Private Sub StatusChanged(ByVal Connected As Boolean, ByVal listen As Boolean)
        If Connected Then
            Label5.Text = "Connected"
        Else
            If listen Then
                Label5.Text = "Online"
            Else
                Label5.Text = "OffLine"
            End If
        End If
        Label6.Text = NetworkServer1.ClientCount
        Label4.Text = NetworkServer1.IP
    End Sub

    Private Sub NetworkServer1_StatusChanged(ByVal Connected As System.Boolean, ByVal Listening As System.Boolean)
        StatusChanged(Connected, Listening)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If NetworkServer1.Connected Then
            Label5.Text = "Connected"
        Else
            If NetworkServer1.Listening Then
                Label5.Text = "Online"
            Else
                Label5.Text = "OffLine"
            End If
        End If
        Label6.Text = NetworkServer1.ClientCount
        Label4.Text = NetworkServer1.IP
    End Sub
End Class
